package com.fengdi.book.easy.Array;

import java.util.ArrayList;

public class PlusOne {
    public int[] plusOne(int[] digits) {
        if(digits == null || digits.length == 0) return digits;
        if(digits.length == 1) return digits[0] == 9 ? new int[]{1,0} : new int[]{digits[0] + 1};

        int len = digits.length;
        ArrayList<Integer> list = new ArrayList<>();

        for(int digit : digits) {
            list.add(digit);
        }

        this.plus(list, list.size() - 1);

        return list.stream().mapToInt(i -> i).toArray();


    }

    private void plus(ArrayList<Integer> list, int index) {
        if(index < 0) {
            list.addFirst(1);
            return;
        }

        if(list.get(index) + 1 < 10) {
            list.set(index, list.get(index) + 1);
            return;
        }

        if(list.get(index) + 1 == 10) {
            list.set(index, 0);
            this.plus(list, index - 1);
        }
    }
}
